package com.activity.activitybpm.controller.bpm.task;

import cn.dev33.satoken.util.SaResult;
import com.activity.activitybpm.controller.bpm.task.vo.TaskInfoVo;
import lombok.extern.log4j.Log4j2;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/task")
@Log4j2
public class ActivityTaskController {

    @PostMapping(value = "/list")
    public SaResult taskList() {
        // 1. 实例化流程引擎实例
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        List<Task> list = taskService.createTaskQuery().list();
        List<TaskInfoVo> taskInfoVos = new ArrayList<>();
        list.forEach(t->{
            log.info("=============任务: {}", t.getName());
            log.info("=============任务Id: {}", t.getId());
            log.info("=============任务办理人: {}", t.getAssignee());
            taskInfoVos.add(new TaskInfoVo(t.getId(), t.getName(),t.getAssignee()));
        });
        return SaResult.ok().setData(taskInfoVos);
    }

}
